// ml:run = $bin < input
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

std::string pat;
std::string s;
int pos = -1;
bool good[26];

bool match(std::string const& pat, std::string const& s)
{
    if (pat.size() != s.size()) return false;
    int len = pat.size();
    for (int i = 0; i < len; i++) {
        if (pat[i] == '?') {
            if (!good[s[i] - 'a']) return false;
        } else {
            if (pat[i] != s[i]) return false;
        }
    }
    return true;
}

bool check()
{
    if (pos == -1) return match(pat, s);

    std::string p1(pat.begin(), pat.begin() + pos);
    if ((int)s.size() < pos) return false;
    if (!match(p1, std::string(s.begin(), s.begin() + pos))) return false;

    auto left = pat.size() - pos - 1;
    std::string p2(pat.rbegin(), pat.rbegin() + left);
    if (s.size() < p1.size() + p2.size()) return false;
    if (!match(p2, std::string(s.rbegin(), s.rbegin() + left))) return false;

    for (size_t i = pos; i < s.size() - left; i++)
        if (good[s[i] - 'a']) return false;
    return true;
}

int main()
{
    std::ios::sync_with_stdio(false);
    std::string gs;
    std::cin >> gs;
    for (auto ch : gs) good[ch - 'a'] = true;
    std::cin >> pat;
    int n; std::cin >> n;
    auto p = pat.find('*');
    if (p != std::string::npos) pos = p;

    for (int i = 0; i < n; i++) {
        std::cin >> s;
        if (check()) std::cout << "YES\n";
        else std::cout << "NO\n";
    }
}

